import wx
import os
from time import sleep

import scan

class display(wx.Frame):

    def __init__(self):
        self.app = wx.App()
        wx.Frame.__init__(self, None, -1, "JXQT Checker - ver 2.0", size=(500,300))
        self.panel = wx.Panel(self)
        self.icon = wx.Icon("./window.ico", wx.BITMAP_TYPE_ICO)
        self.SetIcon(self.icon)

        self.Show(True)
        self.Center(True)

    def __exit(self, event):
        exit()

    def __openFile(self, event):
        self.openFile_Window = wx.FileDialog(self.panel, "选择一个xml或xls文件", "./", wildcard="XML Files(*.xml)|*.xml", style=wx.FD_OPEN)
        self.openFile = self.openFile_Window.ShowModal()
        self.filePath = self.openFile_Window.GetPath()

    def __menu(self):
        self.menuBar = wx.MenuBar()
        self.menu = wx.Menu()

        self.openFile_menuButton = self.menu.Append(-1, u"Open...\tCtrl+O")
        self.Bind(wx.EVT_MENU, self.__openFile, self.openFile_menuButton)

        self.menu.AppendSeparator()

        self.exit_menuButton = self.menu.Append(-1, u"Exit\tEsc")
        self.Bind(wx.EVT_MENU, self.__exit, self.exit_menuButton)

        self.menuBar.Append(self.menu, "File")
        self.SetMenuBar(self.menuBar)

    def __staticText(self):
        self.path_text = wx.StaticText(self.panel, -1, label="File Path:", style=wx.ALIGN_LEFT, pos=(20,20))

        self.statue_text = wx.StaticText(self.panel, -1, label="Statue:     Waiting...", style=wx.ALIGN_LEFT, pos=(20,80))

        self.result_text = wx.StaticText(self.panel, -1, label="Result:     ", style=wx.ALIGN_LEFT, pos=(20,130))

    def __textCtrl(self):
        self.path_input = wx.TextCtrl(self.panel, -1, style=wx.TE_LEFT|wx.TE_PROCESS_ENTER, size=(350,25), pos=(80,15))

    def __button(self):
        self.sureButton = wx.Button(self.panel, -1, label="Sure", pos=(380,180))
        self.exitButton = wx.Button(self.panel, -1, label="Exit", pos=(290,180))

    def __enterEvent(self, event):
        try:
            self.filePath = self.path_input.GetValue()
            print(self.filePath)
            self.statue_text.SetLabel("Statue:     Processing...")
        except Exception as e:
            print(e)
            self.statue_text.SetLabel("Statue:     WRONG -- Failed to get the content in the textControl !")

        # 调用 scan 运行主程序
        try:
            mainProgram = scan.scanFile(self.filePath)
            mainProgram.start()
            self.__endEvent()

        except FileNotFoundError:
            if self.filePath:
                self.statue_text.SetLabel("Statue:     WRONG -- No such file or directory: \n\t\t" + self.filePath)
            else:
                self.statue_text.SetLabel("Statue:     WRONG -- The input box is empty!")

        except OSError:
            self.statue_text.SetLabel("Statue:     WRONG -- Please check the format of the directory: \n\t\t" + self.filePath)

    def __endEvent(self):
        self.statue_text.SetLabel("Statue:     Successfully!")

        self.result_text.SetLabel("Result:     The result has already saved to the root of this program.")

    def __eventHandler(self):
        self.panel.Bind(wx.EVT_TEXT_ENTER, self.__enterEvent)
        self.sureButton.Bind(wx.EVT_BUTTON, self.__enterEvent)

        self.exitButton.Bind(wx.EVT_BUTTON, self.__exit)
    
    def __mainloop(self):
        self.app.MainLoop()

    def start(self):
        self.__menu()
        self.__staticText()
        self.__textCtrl()
        self.__button()
        self.__eventHandler()
        self.__mainloop()




if __name__ == "__main__":
    program = display()
    program.start()